﻿/**
 * DataDrivenSuite.cs
 *
 * Copyright (C) 2008,  iron9light
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Reflection;
using NUnit.Core;

namespace NUnitExtension.DataDriven.AddIn
{
    public class DataDrivenSuite : TestSuite
    {
        private static string GetParentName(MethodInfo method)
        {
            if (method == null)
                throw new ArgumentNullException("method");

            return method.DeclaringType.ToString();
        }

        private static string GetTestName(MethodInfo method)
        {
            if (method == null)
                throw new ArgumentNullException("method");

            return method.Name;
        }

        public DataDrivenSuite(MethodInfo method)
            : base(GetParentName(method), GetTestName(method))
        {
        }

        public override TestResult Run(EventListener listener, ITestFilter filter)
        {
            if (this.Parent != null)
                this.Fixture = this.Parent.Fixture;

            return base.Run(listener, filter);
        }

        //public new void Add(Test test)
        //{
        //    base.Add(test);
        //    TestNameUtility.ReplaceBase(test.TestName, this.TestName.FullName);
        //}

        public void InsertNum()
        {
            int count = this.TestCount;

            if(count == 0)
                return;

            int len = (int)Math.Log10(count)+1;
            int num = 1;
            foreach (Test test in this.Tests)
                TestNameUtility.InsertNum(test.TestName, num++, len);
        }

        protected override void DoOneTimeSetUp(TestResult suiteResult)
        {
        }

        protected override void DoOneTimeTearDown(TestResult suiteResult)
        {
        }
    }
}